package com.cmxh.shipper.controller;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.oss.OSSClient;
import com.aliyun.teaopenapi.models.Config;
import com.cmxh.shipper.pojo.AccountAuthentication;
import com.cmxh.shipper.service.AccountAuthenticationService;
import com.cmxh.shipper.util.Code;
import com.cmxh.shipper.util.OSSUtil;
import com.cmxh.shipper.util.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/*
 *
 *开户许可证认证Controller
 * */
@RestController
@RequestMapping("/accountAuthentication")
public class AccountAuthenticationcontroller {
    @Autowired
    private AccountAuthenticationService accountAuthenticationService;
    @Autowired
    private OSSUtil ossUtil;

    @RequestMapping("/select/{eid}")
    public Result select(@PathVariable Integer eid) {
        AccountAuthentication accountAuthentication = accountAuthenticationService.selectByPrimaryKey(eid);
        if (accountAuthentication != null) {
            return new Result(Code.GET_OK, accountAuthentication, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @RequestMapping("/update")
    public Result update(MultipartFile file) throws Exception {
        System.out.println(file);
        InputStream originalFilename = file.getInputStream();
        String newname = UUID.randomUUID().toString().substring(24) + "-" + originalFilename;
        OSSClient xx1 = new OSSClient(ossUtil.getUrl(), ossUtil.getId(), ossUtil.getMiyao());
        xx1.putObject(ossUtil.getTong(), newname, file.getInputStream());
        String url1 = ossUtil.getUrl();
        String[] split = url1.split("//");
        String url = split[0] + "//" + ossUtil.getTong() + "." + split[1] + "/" + newname;
        // 使用身份证识别接口
        // 初始化请求对象
        RecognizeBankAccountLicenseRequest pp = new RecognizeBankAccountLicenseRequest();
        pp.setUrl(url); // 设置要识别的图片URL


        // 配置阿里云客户端
        Config config = new Config().setAccessKeyId(ossUtil.getId()).setAccessKeySecret(ossUtil.getMiyao()).setEndpoint("ocr-api.cn-hangzhou.aliyuncs.com");

        Client xx = new Client(config);

        // 发起请求并获取响应
        RecognizeBankAccountLicenseResponse ee = xx.recognizeBankAccountLicense(pp);
        RecognizeBankAccountLicenseResponseBody body = ee.getBody();

        System.out.println("请求编号: " + body.getRequestId());
        System.out.println("错误码: " + body.getCode());
        System.out.println("响应数据: " + body.getData());
        System.out.println("错误信息: " + body.getMessage());
        ObjectMapper oo = new ObjectMapper();
        Map dataMap = oo.readValue(body.getData(), Map.class);
        Map ocrData = (Map) dataMap.get("data");
        System.out.println("数据==========================================================");
        System.out.println(ocrData);

        AccountAuthentication accountAuthentication = new AccountAuthentication();
        accountAuthentication.setAccountName(ocrData.get("customerName").toString());
        accountAuthentication.setBankAccountNumber(ocrData.get("bankAccount").toString());
        accountAuthentication.setOpeningBank(ocrData.get("depositaryBank").toString());
        accountAuthentication.setPermitImagePath(url);
        return new Result(Code.UPDATE_OK, accountAuthentication, "上传成功");
    }

    @RequestMapping("/add")
    public Result add(@RequestBody AccountAuthentication accountAuthentication) {
        System.out.println(accountAuthentication);
        int insert = accountAuthenticationService.insert(accountAuthentication);
        if (insert > 0) {
            return new Result(Code.SAVE_OK, "添加成功");
        } else {
            return new Result(Code.SAVE_ERR, "添加失败");
        }


    }

}